<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class PollOptionsController extends AppController {

    var $name = 'poll_options';
    var $components = array(
        'Session', 'Alias'
    );
    var $helpers = array(
        'Published'
    );
    var $uses = array(
        'Option', 'Poll'
    );

    function index() {

        $this->set('title_for_layout', Configure::read('site_name') . ' - Câu trả lời');
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;
        $search = !empty($this->params['form']['search']) ? $this->params['form']['search'] : false;
        $conditions = array();

        //search
        if ($search) {
            $conditions['OR'] = array(
                'Option.title LIKE' => "%$search%",
            );
        }
        $this->paginate = array(
            'conditions' => $conditions,
            'limit' => 50,
            'order' => array(
                'Option.ordering' => 'ASC'
            ),
        );

        $data = $this->paginate('Option');
        $this->set('total', $this->params['paging']['Option']['count']);
        $this->set('total_inpage', sizeof($data));
        $this->set('SEARCH_KEYWORD', $search);

        $this->set('data', $data);
        $this->set('limit_page', $this->paginate['limit']);
        $this->set('start_page', $page);
        $this->Option->writePoll();
    }

    function remove() {
        $conditions = array(
            "{$this->Option->alias}.{$this->Option->primaryKey} IN (" . implode(',', $this->params['form']['cid']) . ')'
        );

        if ($this->Option->deleteAll($conditions, true)) {
            $this->Session->setFlash(sprintf("Xóa thành công %d Option", sizeof($this->params['form']['cid'])), 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash('Không thể xóa', 'msg', array(), 'error');
        }
        $this->redirect(array('action' => 'index'));
    }

    function edit() {
        $params = array_merge($this->params['form'], $this->params['named']);
        if (isset($params['cid']) && is_array($params['cid'])) {
            $this->Option->id = $params['cid'][0];
        } else {
            $this->Option->id = array_key_exists('id', $params) ? $params['id'] : 0;
        }

        $this->set('title_for_layout', Configure::read('site_name') . ' - Chỉnh sửa thông tin');

        if (empty($this->data)) {
            $fields = array(
                '*'
            );
            $this->data = $this->Option->read($fields);
        }
        $this->set('poll_data', $this->Option->Poll->getListAll());
    }

    function add() {
        $this->set('title_for_layout', Configure::read('site_name') . ' - thêm câu trả lời');
        $this->set('poll_data', $this->Option->Poll->getListAll());
    }

    function save() {
        $data = $this->data['Option'];

        if (!empty($data['id'])) {
           
            $data['user_modified'] = $this->Auth->user('id');
            $data['user_modified_ip'] = $_SERVER['REMOTE_ADDR'];
        } else {
            $data['alias'] = $this->Alias->alias($data['title']);
            $data['user_created'] = $this->Auth->user('id');
             $data['ordering'] = $this->Option->nextOrder();
            $data['user_created_ip'] = $_SERVER['REMOTE_ADDR'];
        }

        if ($this->Option->save($data)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Option->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('action' => 'index'));
    }

    function cancel() {
        $this->redirect(array('action' => 'index'));
    }

    function published() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 1,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );
        }

        if ($this->Option->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Option->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array( 'action' => 'index'));
    }

    function unpublished() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 0,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );
        }

        if ($this->Option->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Option->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('action' => 'index'));
    }

    function saveorder() {
        $ordering = $this->params['form']['order'];

        foreach ($ordering AS $id => $order) {
            $fields = array(
                'ordering' => $order,
                'id' => $id,
                'user_modified' => $this->Auth->user('id'),
                'user_modified_ip' => $_SERVER['REMOTE_ADDR']
            );

            $this->Option->saveAll($fields);
        }
        $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');

        $this->redirect(array('action' => 'index'));
    }

}

?>
